package com.It;

import java.util.Scanner;
import java.util.Arrays;

public class SeqList {
    int[] a;
    int len = 0;

    public static void main(String[] args) {
        SeqList seq = new SeqList(50);
        System.out.println("请输入一组数据,以0结束");
        seq.append();
        System.out.println("输入数据为:");
        seq.show();
        System.out.println();
        System.out.println("输入要插入的数字及位置:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int index = sc.nextInt();
        seq.Insert(num, index);
        System.out.println("插入后的数据为:");
        seq.show();
        System.out.println();
        System.out.println("升序排序后的数据为:");
        seq.AscendingSort();
        seq.show();

    }


    //顺序表基本操作
    //----------------------------------------------------------
    //初始化
    SeqList(int max) {
        a = new int[max];
    }

    //添加
    void append() {
        int i = len;
        int x;
        Scanner sc = new Scanner(System.in);
        x = sc.nextInt();
        while (x != 0 && i < a.length) {
            a[i] = x;
            i++;
            x = sc.nextInt();
        }
        len = i;
    }


    //插入
    public void Insert(int num, int index) {
        if (index < 0 || index > len || index == a.length)
            return;
        for (int i = len - 1; i >= index; i--) {
            a[i + 1] = a[i];
        }
        a[index] = num;
        len++;
    }

    //排序
    public void AscendingSort() {
        Arrays.sort(a, 0, len);
    }

    //输出
    void show() {
        for (int i = 0; i < len; i++)
            System.out.print(a[i] + " ");
    }
    //----------------------------------------------------------


}
